package com.kunsan.compose.compoents.home

import android.util.Log
import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxHeight
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.pager.HorizontalPager
import androidx.compose.foundation.pager.rememberPagerState
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.AddCircle
import androidx.compose.material.icons.filled.MailOutline
import androidx.compose.material.icons.outlined.MoreVert
import androidx.compose.material3.Icon
import androidx.compose.material3.IconButton
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Tab
import androidx.compose.material3.TabRow
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.derivedStateOf
import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.draw.shadow
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.navigation.NavHostController
import androidx.navigation.compose.rememberNavController
import com.kunsan.compose.R
import com.kunsan.compose.Screen
import com.kunsan.compose.ui.theme.secondColor

/**
 *  author : zqf
 *  date : 2024/11/25 21:06
 */
@Composable
fun MainScreen(navController: NavHostController) {

    Column(modifier = Modifier
        .fillMaxSize()
        .background(secondColor)) {
        Column(
            modifier = Modifier
                .fillMaxWidth()
        ) {
            Spacer(modifier = Modifier.height(30.dp))
            Box(
                modifier = Modifier
                    .fillMaxWidth(0.33f)
                    .padding(end = 10.dp)
                    .shadow(
                        elevation = 8.dp,
                        shape = RoundedCornerShape(16.dp),
                        clip = false
                    )
                    .clip(RoundedCornerShape(16.dp))
                    .background(Color.White)
                    .align(Alignment.End)
            ) {
                Row(
                    modifier = Modifier
                        .align(Alignment.Center)
                        .padding(horizontal = 16.dp),
                    horizontalArrangement = Arrangement.spacedBy(12.dp)
                ) {
                    IconButton(modifier = Modifier.weight(1f), onClick = {}) {
                        Icon(Icons.Default.AddCircle, contentDescription = null)
                    }

                    IconButton(modifier = Modifier.weight(1f), onClick = {}) {
                        Icon(Icons.Default.MailOutline, contentDescription = null)
                    }

                    IconButton(modifier = Modifier.weight(1f), onClick = {}) {
                        Icon(Icons.Outlined.MoreVert, contentDescription = null)
                    }
                }

            }

            Spacer(modifier = Modifier.height(10.dp))

            Row(modifier = Modifier, verticalAlignment = Alignment.CenterVertically) {
                Image(
                    painter = painterResource(R.drawable.ic_launcher_background),
                    contentDescription = "Circular Image",
                    modifier = Modifier
                        .padding(start = 16.dp)
                        .size(76.dp) // 设置图片的大小
                        .clip(CircleShape),

                    )

                Spacer(modifier = Modifier.width(16.dp))

                Column {
                    Text("已加入5台设备", style = TextStyle(fontSize = 14.sp))
                    Text("二大爷的设备", style = TextStyle(fontSize = 24.sp))
                }
            }

            Spacer(modifier = Modifier.height(30.dp))
        }
        DeviceList()
        Column(modifier = Modifier.fillMaxHeight(0.2f)) {
            Image(contentDescription = "mic", painter = painterResource(R.drawable.icon_microphone))
            Image(modifier = Modifier.clickable {
                navController.navigate(Screen.Room.route)
            }, contentDescription = "camera", painter = painterResource(R.drawable.icon_camera))
        }
    }

}

@Composable
fun DeviceList() {
    val entries = arrayListOf("111", "222", "333")
    val pagerState = rememberPagerState(pageCount = {
       3
    })
    val selectedTabIndex = remember { derivedStateOf { pagerState.currentPage } }

    Column(
        modifier = Modifier
            .fillMaxWidth()
            .clip(RoundedCornerShape(topStart = 36.dp, topEnd = 36.dp))
            .background(Color.White)
    ) {
        TabRow(
            selectedTabIndex = selectedTabIndex.value,
            modifier = Modifier.fillMaxWidth()
        ) {
            entries.forEachIndexed { index, currentTab ->
                Tab(
                    selected = selectedTabIndex.value == index,
                    selectedContentColor = MaterialTheme.colorScheme.primary,
                    unselectedContentColor = MaterialTheme.colorScheme.outline,
                    onClick = {

                    },
                    text = { Text(text = currentTab) },
                    icon = {

                    }
                )
            }
        }

        HorizontalPager(
            state = pagerState,
            modifier = Modifier
        ) {
            Box(
                modifier = Modifier.fillMaxWidth(),
                contentAlignment = Alignment.Center
            ) {

                Log.d("VideoCell", "pageIndex ${selectedTabIndex.value}")

                Text(text = entries[selectedTabIndex.value])
            }
        }
    }
}

@Composable
@Preview(showBackground = true)
fun PreviewHomeScreen() {
    MainScreen(rememberNavController())

}